<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">

</head>
<body>
<input type="text" id="numOne" placeholder="请输入第一个数"/>
<select id="operation">
    <option>+</option>
    <option>-</option>
    <option>*</option>
    <option>/</option>
    <option>%</option>
</select>
<input type="text" id="numTwo" placeholder="请输入第二个数"/>
<input type="button" value="计算" onclick="result()"/>
<hr/>
计算结果:<span id="result"></span>

<script>
    function result() {
        var numOne = document.getElementById("numOne").value;
        var numTwo = document.getElementById("numTwo").value;
        var operation = document.getElementById("operation").value;
        var re = /^[0-9]+.?[0-9]*/;//判断字符串是否为数字//判断正整数/[1−9]+[0−9]∗]∗/
        if (re.test(numOne) || re.test(numTwo)) {  //判断输出的是否是数字
            if (operation == "/") {
                if (numTwo == 0) {
                    alert("除数不能为0");
                } else {
                    //toFixed(2)代表保留小数点后两位
                    var result = eval(numOne + operation + numTwo);
                    document.getElementById("result").innerText = result.toFixed(2);
                }
            } else {
                var result = eval(numOne + operation + numTwo);
                document.getElementById("result").innerText = result.toFixed(2);
            }

        } else {
            alert("请输入大于零的数字");
        }
    }
</script>
</body>
</html>
